unit log;

interface
uses CoreI;
procedure Log_init(FName:string; Overwrite:boolean; MinLevel:TLogLevel);
Procedure Log_Write(const message:PChar; level:TLogLevel); overload; cdecl;
Procedure Log_Write(const message:PChar; level:TLogLevel; args:array of const); overload; cdecl;
implementation
uses sysutils;
var
LogName:string='';
LogMinLevel:TLogLevel;
procedure Log_init;
var f:text;
begin
if fname='' then exit;
try
assign(f, fname);
if overwrite or (not fileexists(fname)) then rewrite(f);
close(f);
logname:=fname;
LogMinLevel:=minlevel;
log_write(PChar('Starting log: ' + DateToStr(now) + ' ' + TimeToStr(time)), ll_info);
except
Core_Error('Log_Init: can not open file', ECore_Init);
end;
end;
Procedure Log_Write(const message:PChar; level:TLogLevel); overload; cdecl;
var f:Text;
begin
           if level > LogMinLevel then exit;
if LogName <> '' then begin
assign(f, LogName);
append(f);
writeln(f, Message);
close(f);
end else writeln(message);
end;
Procedure Log_Write(const message:PChar; level:TLogLevel; args:array of const); overload; cdecl;
begin
log_write(PChar(format(Message,args)), level);
end;

end.
